<template>
    <div>
        <a-modal
            :title="modalTitle"
            :closable="true"
            :destroyOnClose="true"
            :visible="handleModalVisible"
            :confirm-loading="confirmLoading"
            width="70%"
            @cancel="closeVisible"
        >
            <template slot="footer" style="text-align:center;">
                <a-button @click="onSubmit('保存')">
                    保存
                </a-button>
                <a-button class="blue-btn" @click="onSubmit('提交')">
                    提交
                </a-button>
            </template>
            <a-form-model
                ref="ruleForm"
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
                class=""
            >
                <a-form-model-item label="参与人员" prop="fcyry">
                    <a-textarea v-model="form.fcyry" @click="checkMan('fcyry',form.fchryid)" :rows="4" style="width: 100%;"/>
                </a-form-model-item>
                <a-form-model-item label="地点" prop="fdd">
                    <a-input v-model="form.fdd" style="width: 100%;"/>
                </a-form-model-item>
                <a-form-model-item label="结果" prop="fjg">
                    <a-select v-model="form.fjg" style="width: 100%;">
                        <a-select-option value="通过">
                            通过
                        </a-select-option>
                        <a-select-option value="不通过">
                            不通过
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="结果描述" prop="fjgms">
                    <a-textarea v-model="form.fjgms" :rows="10" style="width: 100%;"/>
                </a-form-model-item>
                <a-form-model-item label="进行的方式" prop="fjhdfs">
                    <a-input v-model="form.fjhdfs" style="width: 100%;"/>
                </a-form-model-item>
                <a-form-model-item label="附件">
                    <a-upload style="display: inline-block;" :showUploadList="false" :beforeUpload="beforeUpload" action="" @change="uploadClick">
                        <a-button class="blue-btn">上传附件</a-button>
                    </a-upload>
                    <p v-for="(item,index) in filesListData" :key="index">
                        <span style="display:inline-block;min-width:100px;">{{item.fmc}}</span>
                        <a-button style="margin-right: 10px;" class="blue-btn" @click="openFiles(item)" >预览</a-button>
                        <a-button style="margin-right: 10px;" class="blue-btn" @click="downloadFiles(item)" >下载附件</a-button>
                        <a-button
                            class="red-btn"
                            style="margin-right: 10px;"
                            @click="deleteFiles(item.id)" >
                            删除附件
                        </a-button>
                    </p>
                </a-form-model-item>
            </a-form-model>
        </a-modal>
        <personnelList ref="personnelListRef" :modaObj="modaObj" @checkPersonnel="checkPersonnel"></personnelList>
    </div>
</template>
<script>
import personnelList from '../../administrativeOffice-new/modal/personnelList.vue'
import {saveFlow,submitFlow,fjupload,fjedit,openFile,downLoadOa,fjlist,deleleMinioFile,selectdyfzjl,} from './service/index'
import { sendSjSms } from '../../assessCheck/trainingManagement/service';
import blobType from '@/common/blobType'
import YchConstant from '@/api/YchConstant'
export default {
    props: {
        passObj:{
            type:[Array,Object],
            default: () => {},
            required:false
        },
    },
    components:{
        personnelList
    },
    data() {
      return {
            handleModalVisible: false,
            confirmLoading: false,
            labelCol: { span: 4 },
            wrapperCol: { span: 16 },
            form: {
                fcyry: "",
                fchryid: '',
                fdd: "",
                fjg: "",
                fjgms: "",
                fjhdfs: "",
                jobid: "",
                id: '',
                oaid: ''
            },
            kongForm: {},
            rules: {
                fcyry: [{ required: true, message: '请输入参与人员', trigger: 'blur' }],
                fdd: [{ required: true, message: '请输入地点', trigger: 'blur' }],
                fjg: [{ required: true, message: '请输入结果', trigger: 'change' }],
                fjgms: [{ required: true, message: '请输入结果描述', trigger: 'blur' }],
                fjhdfs: [{ required: true, message: '请输入进行的方式', trigger: 'blur' }],
            },
            modalTitle: '',
            modaObj: {},
            filesListData: [],
        };
    },
    watch: {
        passObj(newval,oldVal){
            //console.log(newval,newval.fpid)
            if(newval.fpid){
                this.modalTitle = newval.fhjmc
                this.getselectdyfzjl(newval.fpid,newval.jobid,newval.fhjmc)
            }
        },
    },
    mounted(){
        this.kongForm = JSON.parse(JSON.stringify(this.form))
    },
    methods: {
        showModal() {
            this.handleModalVisible = true;
        },
        getselectdyfzjl(id,jobid,name){
            let data = {
                frdsqjlid: id,
                hjmc: name,
                jobid: jobid
            }
            selectdyfzjl(data).then(res => {
                //console.log(res)
                this.form = res.result.dy
                this.form.oaid = res.result.dy.id
                this.form.jobid = jobid
                //console.log('55555555',this.form)
                this.filesListData = res.result.fjList
            })
        },
        checkMan(place,man){
            this.$nextTick(_ => {
                this.modaObj = {place: place,oldId: man==null?'':man.split(',')}
                this.$refs.personnelListRef.personnelListVisible = true
            })
        },
        // 选择人员
        checkPersonnel(place,id,name){
            this.$refs.personnelListRef.personnelListVisible = false
            const data = this.form
            data[place] = ''
            data['fchryid'] = ''
            data[place] = name.toString()
            data['fchryid'] = id.toString()
        },
        beforeUpload(e){
            return false
        },
        uploadClick({file}){
            let that = this
            if(typeof file.uid !== 'undefined') {
                const fileData = new FormData();
                fileData.append("file",file);
                fileData.append("oaid",that.form.jobid);
                //console.log(fileData)
                fjupload(fileData).then(res => {
                    //console.log('fileData',res)
                    if (res.success) {
                        that.$message.success(res.message)
                        fjlist({foaid:that.form.jobid}).then(res => {
                            that.filesListData = res.result.records
                        })
                    } else {
                        that.$message.warning(res.message)
                    }
                })
            }
        },
        // 预览
        openFiles(item){
          if(item.fwjlx.indexOf('pdf') !== -1){
              //console.log()
              window.open(
                  bootConfig[process.env.NODE_ENV].locality_ApI2+YchConstant.SPZX_OA+'/oaFjzyk/downLoadOa?id='+item.id
              )
            } else {
              openFile({id:item.id}).then(res => {
                if(!res.success){
                      this.$message.warning('当前附件类型不支持预览,请下载查看')
                } else {
                    window.open(res.result.wordUrl, '_blank')
                }
              })
            }
        },
        // 下载附件
        downloadFiles(val){
        //console.log('下载',val)
        var that = this
        var fileName = val.fmc
        downLoadOa({id:val.id}).then((data) => {
            // //console.log(data)
            if (!data) {
                that.$message.warning('文件下载失败')
                return
            }
            let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf(val.fwjlx)] }))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
            // this.vxModal.visabled = false;
        })
        },
        // 删除附件
        deleteFiles(idd){
            //console.log('idd',idd)
            deleleMinioFile({id:idd}).then(res => {
                if(res.code == 200){
                this.$message.success('删除成功')
                fjlist({foaid:this.form.jobid}).then(res => {
                    this.filesListData = []
                    this.filesListData = res.result.records
                })
                } else {
                this.$message.warning(res.message)
                }
            })
        },
        onSubmit(str) {
            this.confirmLoading = true;
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.confirmLoading = false;
                    let data = this.form
                    if(str == '保存'){
                        saveFlow(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    } else if(str == '提交'){
                        let data1 = {
                            ...data,
                            currentNode: "",
                            dbrid: "",
                            lcmbCode: "",
                            map: {},
                            nextNode: "",
                            nextNodeId: "",
                            opinion: "",
                            smgflag: ""
                        }
                        submitFlow(data1).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                this.closeVisible()
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    }
                }
            })
        },
        closeVisible(e) {
            this.$emit('success')
            this.handleModalVisible = false;
            this.form = JSON.parse(JSON.stringify(this.kongForm))
            this.filesListData = []
        },
    },
};
</script>
